package epoch.db.config;

import epoch.db.gamedb.provider.DataSourceProviderType;
import epoch.game.core.util.Prop;

/**
 * 数据库配置
 * 
 * @author will
 * @date 2016年7月28日 下午1:41:52
 */
public class DBConfig {

	public static final String DRIVER_KEY = "db.driver";
	public static final String URL_KEY = "db.url";
	public static final String USERNAME_KEY = "db.user";
	public static final String PASSWORD_KEY = "db.password";
	public static final String PROVIDER_KEY = "db.provider";
	public static final String AGENT_TYPE_KEY = "db.agentType";
	public static final String ENTITY_PACKAGE_KEY = "db.entityPackages";
	public static final String WORK_THREAD_NUM_KEY = "db.workThreadNum";
	public static final String CONFIG_NAME_KEY = "db.configName";
	public static final String DRUID_FILTER_KEY = "db.druid.filter";
	public static final String DRUID_MAX_ACTIVE_KEY = "db.druid.maxActive";
	public static final String DRUID_REMOVE_ABANDONED_TIME_KEY = "db.druid.removeAbandonedTime";
	/** 数据处理线程数 */
	public static int DB_THREAD_NUM = 8;
	// 默认配置文件路径
	private static final String DEFAULT_CONF_PATH = "../config/game_config.properties";

	public String DB_DRIVER;
	public String DB_URL;
	public String DB_USER;
	public String DB_PASSWORD;
	public int AGENT_TYPE;
	// 实体所在的包名
	public String[] entityBasePackages;
	// 工作线程
	public int workThreadNum;
	// 数据源提供程序
	public DataSourceProviderType provider = DataSourceProviderType.DRUID;
	public String configName;
	// druid过滤器
	public String druidFilter;
	// 最大连接数
	public int maxActive = 100;
	// 连接泄漏检测时间（单位：秒）
	public int removeAbandonedTime;

	public static DBConfig load() {
		return load("", DEFAULT_CONF_PATH);
	}

	public static DBConfig load(String prefix) {
		return load(prefix, DEFAULT_CONF_PATH);
	}

	public static DBConfig load(String prefix, String configPath) {
		Prop prop = Prop.load(configPath);
		return load(prefix, prop);
	}

	public static DBConfig load(String prefix, Prop prop){
		DBConfig config = new DBConfig();
		config.DB_DRIVER = prop.get(prefix+DRIVER_KEY).trim();
		config.DB_URL = prop.get(prefix+URL_KEY).trim();
		config.DB_USER = prop.get(prefix+USERNAME_KEY).trim();
		config.DB_PASSWORD = prop.get(prefix+PASSWORD_KEY).trim();
		config.AGENT_TYPE = prop.getInt(prefix+AGENT_TYPE_KEY,0);
		String packs = prop.get(prefix+ENTITY_PACKAGE_KEY);
		if(packs==null){
			config.entityBasePackages= new String[0];
		}else{
			config.entityBasePackages=packs.trim().split(",");
		}
		config.workThreadNum = prop.getInt(prefix+WORK_THREAD_NUM_KEY,1);
		String providerStr = prop.get(prefix+PROVIDER_KEY);
		if(providerStr!=null && !providerStr.trim().isEmpty()){
			DataSourceProviderType providerType = DataSourceProviderType.valueOf(providerStr.trim().toUpperCase());
			if(providerType!=null){
				config.provider=providerType;
			}
		}
		config.configName = prop.get(prefix+CONFIG_NAME_KEY);
		config.druidFilter = prop.get(prefix+DRUID_FILTER_KEY);
		config.maxActive = prop.getInt(prefix+DRUID_MAX_ACTIVE_KEY, config.maxActive);
		config.removeAbandonedTime=prop.getInt(prefix+DRUID_REMOVE_ABANDONED_TIME_KEY, config.removeAbandonedTime);
		return config;
	}
}
